define(function (require, exports, module) {
        exports.init = function (element, cb) {
                var basic=require('basic');
                var jsonAPI = require('json-api');
                $("#title").text('同户注册');
                var reg = /^[1][358]\d{9}$/; //验证手机号码
                var serverCode=null;

                defineModule({
                        m_calc:function()
                        {
                                $("#btnCode").attr("disabled","disabled");
                                var sec=60;
                                var tt=setInterval(function(){
                                        if(sec>0) {
                                                sec=sec-1;
                                                $("#btnCode").text("重新发送"+sec+"'")
                                        }
                                        else  {
                                                clearInterval(tt);
                                                $('#btnCode').removeAttr("disabled").text("获取验证码");
                                        }
                                },1000)
                        },

                        getIdentifyingCode: function () {
                                var phone = $("#mobile").val();
                                if (!reg.test(phone)) {
                                        $spa.showErrorMsg('请输入正确的手机号!');
                                        return false;
                                }

                                jsonAPI.restful('/api/user/getIdentifyingCode', { "mobile": phone,"type": 0}, function (err, result) {
                                        if (jsonAPI.handleErrors(err, result)) {
                                                        serverCode=result.result;
                                                        //result.result
                                                        $module.m_calc();
                                        }
                                });
                        },

                        register:function()
                        {
                                var phone = $("#mobile").val();
                                if (!reg.test(phone)) {
                                        $spa.showErrorMsg('请输入正确的手机号!');
                                        return false;
                                }

                                var password = $("#password").val();
                                if (password.length < 6 || password.length > 18) {
                                        $spa.showErrorMsg('请输入有效的密码,6到18位!');
                                        return false;
                                }
                                var passwordConfirm = $("#passwordConfirm").val();
                                if (password != passwordConfirm) {
                                        $spa.showErrorMsg('两次密码不一致，请重新输入!');
                                        return false;
                                }

                                var checkCode = $("#checkCode").val();
                                if (checkCode.length != 6) {
                                        $spa.showErrorMsg('请输入有效的验证码!');
                                        return false;
                                }

                                var codeMd5=$.md5(checkCode);
                                if (codeMd5!=serverCode) {
                                        $spa.showErrorMsg('验证码错误!');
                                        return false;
                                }

                                var param = {
                                        "mobile": phone, //手机号
                                        "password": $.md5(password),
                                        "identifycode": checkCode
                                }

                                $spa.showLoading('正在为您注册');

                                jsonAPI.restful('/api/user/register', param, function (err, result) {
                                        if (jsonAPI.handleErrors(err, result)) {
                                                $spa.hideLoading();
                                                $spa.showOk('注册成功');
                                                setTimeout(function () {
                                                     $spa.hideOk();
                                                        window.hashTo('#login',{"mobile":phone});
                                                }, 2000);
                                        }
                                        else{
                                                $spa.showErrorMsg('注册失败,请稍后重试!');
                                                $spa.hideLoading();
                                        }
                                });

                        }
                })


                cb && cb();
        };
});